
import datetime
import os
from pathlib import Path
import gym
import json
from docopt import docopt
from itertools import product
from multiprocessing.pool import Pool
import sys
import importlib

sys.path.append('../highway-env/')
import highway_env

sys.path.append('../rl-agents')
from rl_agents.agents.common.factory import agent_factory

from rl_agents.trainer import logger
from rl_agents.trainer.evaluation import Evaluation
from rl_agents.agents.common.factory import load_agent, load_environment


from rl_agents.agents.common.factory import agent_factory
from rl_agents.agents.deep_q_network.pytorch import DQNAgent

BENCHMARK_FILE = 'benchmark_summary'
LOGGING_CONFIG = 'configs/logging.json'
VERBOSE_CONFIG = 'configs/verbose.json'


def main():
    evaluate()


def evaluate():
    """
    Evaluate an agent interacting with an environment
        :param environment_config: the path of the environment configuration file
        :param agent_config: the path of the agent configuration file
        :param options: the evaluation options
    """
    logger.configure(LOGGING_CONFIG)

    # the environment
    env = gym.make("highway-v0")
    obs, done = env.reset(), False

    # print_state_dim = env.observation_space.shape[0]
	# print_action_dim = env.action_space.n
    # print(" print_state_dim  ", print_state_dim )
    # print(" print_action_dim  ",  print_action_dim)

    agent_config = '/home/zy/Desktop/RF_decision/RF_decision_demo/scripts/configs/HighwayEnv/agents/DQNAgent/mlp.json'
    
    if not isinstance(agent_config, dict):
            with open(agent_config) as f:
                agent_config_1 = json.loads(f.read())
    else:
        agent_config_1 = agent_config

    if "__class__" in agent_config_1:
        path = agent_config_1['__class__'].split("'")[1]
        module_name, class_name = path.rsplit(".", 1)


    agent = DQNAgent(env, config=agent_config_1)

    run_directory = None

    run_directory = "{}_{}_{}".format(Path(agent_config).with_suffix('').name,
                                  datetime.datetime.now().strftime('%Y%m%d-%H%M%S'),
                                  os.getpid())

    

    evaluation = Evaluation(env, agent,run_directory=run_directory,num_episodes=int(100))
                          

    # evaluation.train() # train

    evaluation.run_episodes();

    # evaluation.test() # test
  
    return os.path.relpath(evaluation.monitor.directory)


if __name__ == "__main__":
    main()
